package com.spring.validator_test_2;

import lombok.Data;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 *
 * <!--Validator start-->
 <dependency>
 <groupId>javax.validation</groupId>
 <artifactId>validation-api</artifactId>
 <version>1.1.0.Final</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-validator</artifactId>
 <version>5.1.2.Final</version>
 </dependency>
 <dependency>
 <groupId>org.glassfish</groupId>
 <artifactId>javax.el</artifactId>
 <version>3.0.0</version>
 </dependency>
 <!--Validator end-->
 */
@Data
public class Product {


	public static void main(String[] args) {
		Product product = new Product();
		product.setName("name");

		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<Product>> validations = validator.validate(product);
		for (ConstraintViolation<Product> validation : validations) {
			System.out.println(validation.getPropertyPath() + ": "+validation.getMessageTemplate());
		}
	}

	@NotNull(message = "请填写名称")
	private String name;

	@NotNull(message = "请填写数量")
	private String amount;

	@NotNull(message = "请填写价格")
	private String price;


}